<script lang="ts" setup>
import Search from '@/view/task-management/search/index.vue'
import Table from '@/view/task-management/table/index.vue'
import Pagination from 've-pagination/index.vue'
import Add from '@/view/task-management/dialog/add/index.vue'
import Edit from '@/view/task-management/dialog/edit/index.vue'
import {initTableHeight} from '@/assets/js/tableHeight'
import {nextTick, ref} from 'vue'
import {ResData} from '@/interface/ResData'
import {ElMessageBox} from 'element-plus'
import {createEmitter} from '@/assets/js/eventBus'
import {
  deleteTask,
  getTaskByPage,
  insertTask,
  updateTask,
  updateTaskStart,
  updateTaskStatus,
  updateTaskStop
} from "@/api/result/taskInfo.ts";
import {TaskInsertDto} from "@/interface/dto/task/TaskInsertDto.ts";
import {TaskInfoVo} from "@/interface/vo/task/TaskInfoVo.ts";

const emitter = createEmitter()

const pageRef = ref()
const searchRef = ref()
const tableRef = ref()
nextTick(() => {
  initTableHeight(pageRef, searchRef, tableRef, 50)
})
const onSubmit = (form: any) => {
  getTableData(form)
}
const onReset = (form: any) => {
  getTableData(form)
}

const addDialogVisible = ref(false)

const editDialogVisible = ref(false)

const selected = ref()

const onTableClick = (type: any, id: any) => {
  switch (type) {
    case 'add':
      addDialogVisible.value = true
      break
    case 'edit':
      selected.value = id
      editDialogVisible.value = true
      break
    case 'delete':
      onDelete(id)
      break
    case 'start':
      onChangeStart(id)
      break
    case 'stop':
      onChangeStop(id)
      break
    case 'updateStatus':
      onUpdateStatus(id)
      break
    default:
      console.warn('Button type cannot be empty')
      break
  }
}

const onAdd = async (taskDto: TaskInsertDto) => {
  const res = <ResData>await insertTask(taskDto)
  if (res.ok) {
    addDialogVisible.value = false
    emitter.emit('refresh')
  }
}

const onEdit = async (taskDto: TaskInsertDto) => {
  const res = <ResData>await updateTask(taskDto)
  if (res.ok) {
    editDialogVisible.value = false
    emitter.emit('refresh')
  }
}

const onDelete = (id: String) => {
  ElMessageBox.confirm(
      '确认要删除吗？',
      '警告',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
    const res = <ResData>await deleteTask({id: id})
    if (res.ok) {
      emitter.emit('refresh')
    }
  }).catch(() => {
  })
}

const onChangeStart = async (id: string) => {
  ElMessageBox.confirm(
      '确认要启动吗？',
      '警告',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
    const res = <ResData>await updateTaskStart({id: id})
    if (res.ok) {
      emitter.emit('refresh')
    }
  }).catch(() => {
  })
}

const onChangeStop = async (id: string) => {
  ElMessageBox.confirm(
      '确认要停止吗？',
      '警告',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
    const res = <ResData>await updateTaskStop({id: id})
    if (res.ok) {
      emitter.emit('refresh')
    }
  }).catch(() => {
  })

}

const onUpdateStatus = async (id: string) => {
  const res = <ResData>await updateTaskStatus({id: id})
  if (res.ok) {
    emitter.emit('refresh')
  }
}

const pageSize = ref(10)

const handleSizeChange = (val: number) => {
  pageSize.value = val
  emitter.emit('refresh', {pageSize: val})
}

const current = ref(1)

const handleCurrentChange = (val: number) => {
  current.value = val
  emitter.emit('refresh', {page: val})
}


const tableLoading = ref(false)

const tableData = ref<Array<TaskInfoVo>>([])

const total = ref(0)

const getTableData = async (form: {}) => {
  tableLoading.value = true
  const res = <ResData>await getTaskByPage(form)
  tableLoading.value = false
  if (res.ok) {
    tableData.value = res.data.records
    total.value = res.data.total
  }
}
</script>

<template>
  <div ref="pageRef" class="page">
    <el-alert :closable="false" title="所有的操作均为异步操作，如有延迟等为正常现象" show-icon style="margin-bottom: 10px"
              type="warning"/>
    <Search ref="searchRef" @onReset="onReset" @onSubmit="onSubmit"/>
    <Table
        ref="tableRef"
        :loading="tableLoading"
        :tableData="tableData"
        @onTableClick="onTableClick">
      <template #pagination>
        <Pagination
            :current="current"
            :pageSize="pageSize"
            :total="total"
            @handleCurrentChange="handleCurrentChange"
            @handleSizeChange="handleSizeChange"/>
      </template>
    </Table>

    <Add
        :dialogVisible="addDialogVisible"
        @closed="addDialogVisible = false"
        @onSubmit="onAdd"/>

    <Edit
        :id="selected"
        :dialogVisible="editDialogVisible"
        @closed="editDialogVisible = false"
        @onSubmit="onEdit"/>
  </div>
</template>

<style lang="scss" scoped>

</style>
